import { ref } from 'vue'
import { adapter, request } from '@/request'
import { customerListByGet } from '@/core/api/priceSheet'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { Customer } from '@/core/interfaces/customer'

interface Params {
  customerName: string
  sellerId: number
}

export default function getCustomerList() {
  const service = adapter<RequestWrapper<Customer[]>>(request)
  const loading = ref(false)
  const response = ref<Customer[]>([])

  const fetch = (params: Params) => {
    const { url, method } = customerListByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        response.value = res.data.data.map((item) => {
          return {
            ...item,
            label: item.name,
            value: item.id
          }
        })
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
